package com.lqs.sofp.servlet;


import com.alibaba.fastjson.JSON;
import com.lqs.sofp.entity.Text;
import com.lqs.sofp.entity.User;
import com.lqs.sofp.service.TextService;
import com.lqs.sofp.service.impl.TextServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
/**
 * @author cj
 */
@WebServlet(name = "selectTextServlet", value = "/Text/sts")
public class SelectTextServlet extends HttpServlet {
        //private Article article = new Article();

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取session
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("User");
            //调用service查询
            TextService textService = new TextServiceImpl();
            List<Text> textList = textService.getText(user.getUsername());
            //将数据逆序化以实现：使得最新发布的信息会插入在表头。
            Collections.reverse(textList);
            //将数据JSON化
            String jsonString = JSON.toJSONString(textList);
            //响应数据
            response.getWriter().write(jsonString);
        }

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request, response);
        }

}
